package capstoneproject.dudiachi.Main;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import capstoneproject.dudiachi.Controller.IslandArticleTypeManager;
import capstoneproject.dudiachi.Model.IslandArticleType;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.BaseAdapter;
import android.widget.ListView;

public class IslandArticleTypeActivity extends BaseActivity {

	private ArrayList<Bitmap> bitArray;
	private ListView listView;
	private BaseAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		BaseActivity.POS = 3;
		setContentView(R.layout.activity_list);
		
		IslandArticleTypeManager manager = new IslandArticleTypeManager(getApplicationContext());
		bitArray = new ArrayList<Bitmap>();
		for(IslandArticleType item : manager.getListIslandArticleType()){
			try {
				bitArray.add(getBitmapFromAsset("Icons/BienDao/" + item.getiATIcon()));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		listView = (ListView) findViewById(R.id.list);
		adapter = new IslandArticleTypeListAdapter(getApplicationContext(), manager.getListIslandArticleType(), bitArray); 
		adapter.notifyDataSetChanged();
		listView.setAdapter(adapter);
	}

	private Bitmap getBitmapFromAsset(String strName) throws IOException {
		AssetManager assetManager = getAssets();
		BitmapFactory.Options options = new BitmapFactory.Options();
		InputStream istr = assetManager.open("img/" + strName);
		Bitmap bitmap = null;
		for(options.inSampleSize = 1; options.inSampleSize <=32 ; options.inSampleSize++){
			try{
				bitmap = BitmapFactory.decodeStream(istr,null, options);
				Log.d("SUCCESSFULLY", "Decode successfully at sample size: " + options.inSampleSize);
				break;
			}catch(OutOfMemoryError outOfMemoryError){
				Log.e("ERROR_OUT_MEMORY", "out of memory while reading sample size " + options.inSampleSize + " retrying with higher value");
			}
		}
		return bitmap;
	}
}
